<?php

class StudentTransfer_DemoveController extends Zend_Controller_Action
{
    private $_teacherSn;
    public function init()
    {
        $this->_teacherSn = Teacher_Model_Teacher::getSnById($this->view->user->username);
    }

    public function indexAction()
    {
        $form = new StudentTransfer_Form_Demove();
        $form->getElement('date')->setValue(date("Y-m-d"));
        $form->getElement('approve_date')->setValue(date("Y-m-d"));
        //取系統預設值
        $param = new Default_Model_ModulesParam();
        $param_arr = $param->getParamByResourceId('student');

        $form->setDefault('approve_organ',$param_arr['default_approve_organ']);
        $form->setDefault('approve_word',$param_arr['default_approve_word']);
        $form->setDefault('causation',$param_arr['default_causation_out']);
        $this->view->form = $form;
    }


    public function demoveSetAction()
    {
        $form = new StudentTransfer_Form_Demove() ;

        $data = $this->getRequest()->getParams();
        $data['demove']['year'] = $this->view->Semester['year'];
        $data['demove']['semester'] = $this->view->Semester['semester'];


        if ($form->isValid($data)) {
            $row = $data['demove'];
            // 判斷升降級
            if ((int)$row['selClass'] > (int)$row['toClass'])
            $row['causation_kind'] = Student_Model_StudentTransfer::DOWNGRADE;
            else
            $row['causation_kind'] =  Student_Model_StudentTransfer::UPGRADE;
            // 更改學生學期班級記錄  Student_Model_StudentSemester
            // 找到 本學期記錄
            $q = Doctrine_Query::create()
            ->select('sn')
            ->from('Student_Model_StudentSemester')
            ->where('year=?', $row['year'])
            ->andwhere('semester=?', $row['semester'])
            ->andWhere('student_sn=?', $row['studentSn']);
            $res  = $q->fetchOne();
            if ($res) {
                $grade = (int)substr($row['toClass'], 0, 1);
                $classSn = (int)substr($row['toClass'], 1);
                $q = Doctrine_Query::create()
                ->update('Student_Model_StudentSemester')
                ->set('grade', '?', $grade)
                ->set('class_sn', '?', $classSn)
                ->where('sn=?', $res->sn);
                $q->execute();
            }
            // 新增異動資料 Student_Model_StudentTransfer
            $row['student_sn'] = $row['studentSn'];
            $row['teacher_sn'] = $this->_teacherSn;
            $studentTransfer = new Student_Model_StudentTransfer();
            $studentTransfer->fromArray($row);
            $studentTransfer->save();
            // TODO 考慮入學年是否更改
            $this->_helper->json(1);
        }
        else
        $this->_helper->json(array('error'=>$form->getMessages()));
    }

    /**
     * 升降級學生修改
     * @return unknown_type
     */
    public function demoveEditAction()
    {
        $form = new StudentTransfer_Form_Demove() ;
        $form->getElement('selClass')->setOptions(array('required'=>false));
        $form->getElement('studentSn')->setOptions(array('required'=>false));
        $form->getElement('toClass')->setOptions(array('required'=>false));
        $data = $this->getRequest()->getParams();
        $data['demove']['year'] = $this->view->Semester['year'];
        $data['demove']['semester'] = $this->view->Semester['semester'];

        if ($form->isValid($data)) {
            //取出修改 sn
            $sn = $data['sn'] ;
            $row = $data['demove'];
            $q = Doctrine_Query::create()
            ->update('Student_Model_StudentTransfer')
            ->set('date','?',$row['date'])
            ->set('approve_organ','?', $row['approve_organ'])
            ->set('approve_date','?', $row['approve_date'])
            ->set('approve_word','?', $row['approve_word'])
            ->set('approve_number','?', $row['approve_number'])
            ->where('sn=?', $sn);
            // update student_transfer
            $q->execute();

            $this->_helper->json(1);
        }
        else
        $this->_helper->json(array('error'=>$form->getMessages()));
    }

    /**
     * 取出 學期轉入學生資料
     * @return unknown_type
     */
    public function getStudentAction()
    {
        $this->getHelper('layout')->disableLayout();
        $request = $this->getRequest();
        if ($request->getParam('year_semester')) {
            $arr = explode("-",$request->getParam('year_semester'));
            $year = (int)$arr[0];
            $semester =(int) $arr[1];
        }
        else {
            $year = $this->view->Semester['year'];
            $semester = $this->view->Semester['semester'];
        }
        $kind = $request->getParam('kind');
        $q = Doctrine_Query::create()
        ->select('a.*, b.name AS name, b.identification_number AS dentification_number, b.sex AS sex,
          c.student_id AS student_id, c.grade AS grade, c.class_sn AS class_sn, c.number AS number')
        ->from('Student_Model_StudentTransfer a ')
        ->leftJoin('a.Student_Model_Student  b')
        ->leftJoin('b.Student_Model_StudentSemester c')
        ->where('a.year=c.year AND a.semester=c.semester')
        ->andWhere('c.year=?', $year)
        ->andWhere('c.semester=?',$semester)
        ->andWhereIn('a.causation_kind',array(
        Student_Model_StudentTransfer::UPGRADE,
        Student_Model_StudentTransfer::DOWNGRADE
        ))
        ->orderBy('a.date DESC');

        $res = $q->fetchArray();
        $this->view->data = $res;
    }

    /**
     * 班級學生名單
     * @return unknown_type
     */
    public function getstudentsAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->getHelper('ViewRenderer')->setNoRender();
        $yearSemester = $this->getRequest()->getParam('year_semester');
        if (!$yearSemester)
        $yearSemester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        $class_id = $this->getRequest()->getParam('class_id');
        echo $this->view->StudentNameOptions($yearSemester,$class_id);
    }

      /**
     *  取出異動資料
     */
    public function getTransferDataByidAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn');
        $core = new StudentTransfer_Model_Core();
        $res = $core->getTransferDataByid($sn);
        $this->_helper->json($res);
    }


}

